//
//  MovieListViewController.m
//  SELF 豆瓣
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListTableViewCell.h"
#import "AppTools.h"
#import "MBProgressHUD.h"//菊花
#import "MovieDetailViewController.h"
#import "Movie.h"
#import "FlowLayoutController.h"
#import "pass.h"
//数据单例
#import "DataBaseSingleton.h"


@interface MovieListViewController ()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,retain)UITableView *moviewTableView;
//存放model的数组
@property(nonatomic,retain)NSMutableArray *movieArray;
@property(nonatomic,retain)NSMutableArray *imageArray;
@property(nonatomic,retain)NSMutableArray *imageNameArray;
@property(nonatomic,retain)NSMutableArray *movieIdArray;
@property(nonatomic,retain)MBProgressHUD *hub;//菊花

@property(nonatomic,assign)BOOL isUpLoad;
@end

@implementation MovieListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = [pass share].title;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    
    [self.view addSubview:self.moviewTableView];
    [_moviewTableView release];
    [self.moviewTableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"cellMoive"];
    
    
    
    //从数据库中取数据
    
    //开
    [[DataBaseSingleton shareDataBase]openDB];
    
    
    //取
    self.movieArray = [NSMutableArray array];
    
    self.movieArray = [[DataBaseSingleton shareDataBase]selectMovie];
    
    
    

    
    
    
    
    
    
    self.imageArray = [NSMutableArray array];
    self.imageNameArray = [NSMutableArray array];
    self.movieIdArray = [NSMutableArray array];
    NSString *urlstr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
    
    
    
    //让菊花转
    self.hub = [MBProgressHUD showHUDAddedTo:self.moviewTableView animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"加载中...";
    [AppTools getDataWithURL:urlstr block:^(id result) {
        
        // //删
        self.movieArray = [NSMutableArray array];
        
        [[DataBaseSingleton shareDataBase]dropMovieTbele];
        
        
        //表
        
        [[DataBaseSingleton shareDataBase]createMovieTable];
        
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        //判断解析成功()
        
        if ([reason isEqualToString:@"success"])
        {
            for (NSMutableDictionary *dic in array) {
                Movie *movie = [[Movie alloc]init];
                [movie setValuesForKeysWithDictionary:dic];
                
                //存数据到数据库
                
                
                [[DataBaseSingleton shareDataBase]insertMovieStr:movie];
                [self.movieArray addObject:movie];
                
                
                [self.imageArray addObject:movie.pic_url];
                [self.imageNameArray addObject:movie.movieName];
                [self.movieIdArray addObject:movie.movieId];
                [movie release];
                
            }
            
            [self.moviewTableView reloadData];
    
            [self.hub hide:YES];
        }
        else
        {
            
            // 虽然请求成功可是返回的是个失败信息，根据失败信息判断数据是否请求到、用来提示用户：如：网络不给力
            NSLog(@"请求失败");
            
        }
        
        
    }];
    
    
    //设置右侧按钮 进入九宫格页面
    UIImage *image1 = [UIImage imageNamed:@"btn_nav_collection@2x.png"];
    
    //取消背景色渲染
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:image1 style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)]autorelease];
    
}



//懒加载 铺设 tableview
-(UITableView *)moviewTableView
{
    if (_moviewTableView == nil) {
        
        
        self.moviewTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        [self.view addSubview:self.moviewTableView];
        self.moviewTableView.delegate = self;
        self.moviewTableView.dataSource = self;
    }
    
    return _moviewTableView;
    
}


//区的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    
    return 1;
    
    
}


//每个区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.movieArray.count;
    
    
    
    
}

//铺设 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *movieCell = @"cellMoive";
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:movieCell];
    //**********_____缓存先读取本地数据_______*************
    //取出
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.movie = movie;
    return cell;
    
    
}



//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 180;
    
}


//cell 点击

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc]init];
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    [pass share].movieID = movie.movieId;
    
    [self.navigationController pushViewController:movieDetailVC animated:YES];
    
    
}

//右侧点击事件
-(void)rightButton:(UIBarButtonItem *)btn
{
    
    FlowLayoutController *moviejiugongge = [[FlowLayoutController alloc]init];

    [self.navigationController pushViewController:moviejiugongge animated:YES];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Singleton the selected object to the new view controller.
}
*/

@end
